//
//  JLBleManager.h
//  JL_OTA
//
//  Created by 凌煊峰 on 2021/10/11.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <UIKit/UIKit.h>
#import <DFUnits/DFUnits.h>
#import "JLBleEntity.h"
#import "HandleBroadcastPtl.h"

#import <JL_OTALib/JL_OTALib.h>
#import <JL_HashPair/JL_HashPair.h>
#import <JL_AdvParse/JL_AdvParse.h>

NS_ASSUME_NONNULL_BEGIN

@protocol JLBleMangerDelegate <NSObject>

- (void)onScanResult:(NSString *)name identifier:(NSString *)identifier byteData:(NSData *)byteData rssi:(NSNumber *)RSSI;
- (void)onStartScan;
- (void)onStopScan;
- (void)onConnected:(NSString *)address;
- (void)onDisconnected:(NSString *)address;
- (void)onMtu:(NSString *)address mtu:(NSUInteger )mtu;
- (void)onData:(NSString *)address uuid:(NSString *)uuid data:(NSData *)data;

//ota可用
-(void)onOtaAvailable:(NSString *)address mac:(NSString *)mac mandatoryUpgrade:(BOOL)isMandatoryUpgrade;
//开始升级
-(void)onOtaStart;
//升级成功
-(void)onOtaSuccess;
//升级失败
-(void)onOtaFail:(UInt8)msg;
//文件校验进度
-(void)onOtaBootLoaderProgress:(float)progress;
//升级进度
-(void)onOtaProgress:(float)progress;
//强制升级
- (void)onMandatoryUpgrade:(NSString *)address;


@end

#pragma mark - BLE状态通知
extern NSString *kFLT_BLE_FOUND;         //发现设备
extern NSString *kFLT_BLE_PAIRED;        //已配对
extern NSString *kFLT_BLE_CONNECTED;     //已连接
extern NSString *kFLT_BLE_DISCONNECTED;  //断开连接



@protocol JLBleManagerOtaDelegate <NSObject>

@required
/**
 *  ota升级过程状态回调
 */
- (void)otaProgressWithOtaResult:(JL_OTAResult)result withProgress:(float)progress;

@end


@interface JLBleManager : HandleBroadcastPtl

@property (assign, nonatomic) CBManagerState mBleManagerState;
@property (strong, nonatomic) CBPeripheral *__nullable mBlePeripheral;

/// 连接蓝牙是否需要认证配对
@property (assign, nonatomic) BOOL isPaired;

/// 配对秘钥（默认为空）
@property (assign, nonatomic) NSData *pairKey;

/// 连接设备的MTU（单次最大发送数据）
@property (assign, nonatomic) NSInteger bleMtu;

/// 上一次连接的蓝牙UUID
@property (strong, nonatomic) NSString *lastUUID;

/// 上一次连接的蓝牙地址
@property (strong, nonatomic) NSString *__nullable lastBleMacAddress;

@property (strong, nonatomic) JL_OTAManager *otaManager;

@property (assign,nonatomic)BOOL isConnected;

@property (strong, nonatomic) JLBleEntity *__nullable currentEntity;
@property (nonatomic, weak) id<JLBleMangerDelegate> delegate;

/**
 * 单例
 */
+ (instancetype)sharedInstance;

/**
 开始搜索
 */
-(void)startScanBLE:(int)timeout;

/**
 停止搜索
 */
-(void)stopScanBLE;
//断开连接
- (void)disconnect:(NSString *)address;

- (bool)isBleOpen;
//是否已经授权
- (bool)isAuthorized;

//连接
-(void)connectForMac:(NSString *)address;
//写出数据
-(void)write:(NSString *)address serviceUUID:(NSString *)serviceUUID characteristicUUID:(NSString *)characteristicUUID data:(NSData *)data;

-(void)setCharacteristicNotification:(NSString *)address serviceUUID:(NSString *)serviceUUID characteristicUUID:(NSString *)characteristicUUID;


/**
 连接设备
 @param peripheral 蓝牙设备类
 */
-(void)connectBLE:(CBPeripheral*)peripheral;

//设置uuid信息
-(void)setUUIDInfo:(NSDictionary*)uuidInfo;

/**
 断开连接
 */
-(void)disconnectBLE;

-(NSString *)getMethodChannelName;

/**
 使用UUID，重连设备。
*/
-(void)connectPeripheralWithUUID:(NSString *)uuid;


/// HID设备重连
/// - Parameter uuid: 设备 identifyString
-(void)findHid:(NSString *)uuid;

#pragma mark - 杰理蓝牙库OTA流程相关业务

typedef void(^GET_DEVICE_CALLBACK)(BOOL needForcedUpgrade);

typedef void(^CANCEL_CALLBACK)(uint8_t status);

/**
 *  获取已连接的蓝牙设备信息
 */
- (void)getDeviceInfo:(GET_DEVICE_CALLBACK _Nonnull)callback;

/**
 *  OTA升级
 */
- (void)otaFuncWithFilePath:(NSData *)data;
//是否是正在升级
- (BOOL)isOTA;
- (void)otaFuncCancel:(CANCEL_CALLBACK _Nonnull)result;

@end

NS_ASSUME_NONNULL_END
